﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace TankBattle.Models
{
    public class Item : GameObject
    {
        #region Variable

        private int type;
        private int speed;
        private int lifeBonus;
        private int bullet;
        private bool live;
        private int shootSpeed;
        private Sprite sprite;

        #endregion 
 
        #region Properties

        public int TYPE
        {
            get { return type; }
            private set{}
        }
        public int LIFE
        {
            get { return life; }
        }
        public int SPEED
        {
            get { return speed; }
        }
        public bool LIVE
        {
            get { return live; }
        }
        #endregion 

        #region Construction

        public Item()
        { }
        public Item(Texture2D texture, Vector2 position, int width, int height, int typeItem)
        {
            this.type = typeItem;
            Intallization(texture, position, width, height);
        }
        public Item(Texture2D texture, Vector2 position, int typeItem)
        {
            this.type = typeItem;
            Intallization(texture, position, Constant.ITEM_WIDTH, Constant.ITEM_HEIGHT);
        }
       
        #endregion 
        
        #region Function 
        public override bool Update()
        {
            if (sprite.Update())
            {
                sprite.SetCurrentFrame(new Vector2(0, 0));
            };

            if (life > Constant.ZERO) life--;
            if (life == 0 || !live)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        public override void Draw(SpriteBatch spriteBatch)
        {
            sprite.Draw(spriteBatch, position);
        }
        //Thiết lập ảnh hưởng của item lên tank
        public virtual void SetEffect(Tank tank)
        {
            tank.SetSpeed(speed);
            tank.SetBulletType(bullet);
            tank.SetLife(lifeBonus);
            tank.SetShootSpeed(shootSpeed);
        }
        protected override void Intallization(Texture2D texture, Vector2 position, int width, int height)
        {
            base.Intallization(texture, position, width, height);
            live = true;

            sprite = new Sprite(texture,
                new Vector2(width, height),
                new Vector2(Constant.ITEM_SHEET_SIZE_X, Constant.ITEM_SHEET_SIZE_Y),
                Constant.RATE_ITEM, true);
            life = Constant.ITEM_LIFE;

            //khởi tạo thuộc tính cho item
            shootSpeed = Constant.ZERO;
            speed = Constant.ZERO;
            bullet = Constant.ZERO;
            lifeBonus = Constant.ZERO;    
            //Kiểm tra tính tồn tại của Item
            if (!SetItemType()) live = false;
        }
        //Thiết lập loại Item
        protected virtual bool SetItemType()
        {
            switch (this.type)
            {
                case(Constant.ITEM_SPEED):
                    this.speed = Constant.TANK_SPEED_FAST;
                    break;
                case (Constant.ITEM_ROCKET):
                    this.bullet = Constant.BULLET_TYPE_ROCKET;
                    break;
                case (Constant.ITEM_BONUS_LIFE):
                    this.lifeBonus += Constant.TANK_LIFE_BONUS;
                    break;
                case (Constant.ITEM_SHOOT_SPEED):
                    this.shootSpeed += Constant.TANK_SHOOT_SPEED;
                    break;
                default:
                    return false;
            }
            return true;
        }        
        #endregion 
    }
}
